package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.dao.ILimitBusinessDao;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.LimitBusinessMapVo;
import com.manager.plat.vo.LimitBusinessVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class LimitBusinessAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2834501095605184346L;

	private static Logger logger = Logger.getLogger(LimitBusinessAction.class);
	
	private ILimitBusinessDao limitBusinessDao;					//业务配置数据接口
	private IConfigLowerPartnerDao configLowerPartnerDao;		//渠道信息数据接口
	private List<LimitBusinessMapVo> limitBusinessMapVoList;	//业务总量组合信息列表
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息列表
	private LimitBusinessVo limitBusinessVo;					//业务配置总量信息VO
	private String actionPath;									//操作成功跳转路径
	
	public String getLimitBusinessList(){
		List<LimitBusinessVo> limitBusinessVoList = limitBusinessDao.getLimitBusinessList();
		if(limitBusinessVoList != null && limitBusinessVoList.size() > 0){
			Map<String, List<LimitBusinessVo>> limitBusinessVoListMap = new LinkedHashMap<String, List<LimitBusinessVo>>();
			for(LimitBusinessVo limitBusinessVo : limitBusinessVoList){
				int lower_id = limitBusinessVo.getLower_id();
				String lower_name = limitBusinessVo.getLower_name();
				String key = lower_id + ":" + lower_name;
				if(limitBusinessVoListMap.get(key) != null){
					limitBusinessVoListMap.get(key).add(limitBusinessVo);
				}else{
					List<LimitBusinessVo> limitBusinessVoListTemp = new ArrayList<LimitBusinessVo>();
					limitBusinessVoListTemp.add(limitBusinessVo);
					limitBusinessVoListMap.put(key, limitBusinessVoListTemp);
				}
			}
			
			/**遍历Map 把键值放入一个VO里*/
			if(limitBusinessVoListMap != null && limitBusinessVoListMap.size() > 0){
				limitBusinessMapVoList = new ArrayList<LimitBusinessMapVo>();
				for (Map.Entry<String, List<LimitBusinessVo>> entry : limitBusinessVoListMap.entrySet()) {
					String[] keys = entry.getKey().split(":");
					int lower_id = Integer.parseInt(keys[0]);
					String lower_name = keys[1];
					
					/**组装MapVo*/
					LimitBusinessMapVo limitBusinessMapVo = new LimitBusinessMapVo();
					limitBusinessMapVo.setLower_id(lower_id);
					limitBusinessMapVo.setLower_name(lower_name);
					limitBusinessMapVo.setLimitBusinessVoList(entry.getValue());
					
					limitBusinessMapVoList.add(limitBusinessMapVo);
				}
			}
		}else{
			limitBusinessMapVoList = null;
		}
		return SUCCESS;
	}
	
	public String addLimitBusiness(){
		limitBusinessVo = null;
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		return SUCCESS;
	}
	
	public String saveLimitBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		if(limitBusinessVo.getLower_id() != 0){
			try {
				limitBusinessVo.setProvince(URLDecoder.decode(limitBusinessVo.getProvince(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				logger.error(String.format("保存省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
			}
			
			/**分割所有省份*/
			String[] provinces = limitBusinessVo.getProvince().split(":");
			/**定义渠道分省管控列表*/
			List<LimitBusinessVo> limitBusinessList = new ArrayList<LimitBusinessVo>();
			if(provinces != null && provinces.length > 0){
				for(String province : provinces){
					LimitBusinessVo limitBusiness = new LimitBusinessVo();
					limitBusiness.setLower_id(limitBusinessVo.getLower_id());
					limitBusiness.setProvince(province);
					limitBusiness.setDay_limit(limitBusinessVo.getDay_limit());
					limitBusiness.setMonth_limit(limitBusinessVo.getMonth_limit());
					
					limitBusinessList.add(limitBusiness);
				}
			}
			if(limitBusinessList != null && limitBusinessList.size() > 0){
				/**批量保存省份管控配置信息*/
				int count = limitBusinessDao.batchInsertLimitBusiness(limitBusinessList);
				
				ConfigLowerPartnerVo lowerPartner = configLowerPartnerDao.getConfigLowerPartnerById(limitBusinessVo.getLower_id());
				String lower_name = "";
				if(lowerPartner != null){
					lower_name = lowerPartner.getName();
				}
				if(count > 0){
					logger.info(String.format("[%s]新增渠道[%s]省份[%s]管控配置成功", userInfoVo.getLogin_username(), lower_name, limitBusinessVo.getProvince()));
				}else{
					logger.info(String.format("[%s]新增渠道[%s]省份[%s]管控配置失败", userInfoVo.getLogin_username(), lower_name, limitBusinessVo.getProvince()));
				}
			}
			
//			/**根据渠道ID获得渠道下的长号码列表*/
//			List<String> sp_number_list = configLowerPartnerDao.getLowerPartnerSpNumberById(limitBusinessVo.getLower_id());
//			if(sp_number_list != null && sp_number_list.size() > 0){
//				List<LimitBusinessVo> limitBusinessList = new ArrayList<LimitBusinessVo>();
//				for(String sp_number : sp_number_list){
//					if(provinces != null && provinces.length > 0){
//						for(String province : provinces){
//							LimitBusinessVo limitBusiness = new LimitBusinessVo();
//							limitBusiness.setLower_id(limitBusinessVo.getLower_id());
//							limitBusiness.setSp_number(sp_number);
//							limitBusiness.setProvince(province);
//							limitBusiness.setDay_limit(limitBusinessVo.getDay_limit());
//							limitBusiness.setMonth_limit(limitBusinessVo.getMonth_limit());
//							
//							limitBusinessList.add(limitBusiness);
//						}
//					}
//				}
//				
//				if(limitBusinessList != null && limitBusinessList.size() > 0){
//					/**批量保存省份管控配置信息*/
//					int count = limitBusinessDao.batchInsertLimitBusiness(limitBusinessList);
//					
//					if(count > 0){
//						logger.info(String.format("[%s]新增[%s]省份管控配置成功", userInfoVo.getLogin_username(),limitBusinessVo.getProvince()));
//					}else{
//						logger.info(String.format("[%s]新增[%s]省份管控配置失败", userInfoVo.getLogin_username(),limitBusinessVo.getProvince()));
//					}
//				}
//			}
		}
		
		actionPath = "limitBusiness/getLimitBusinessList.action";
		return SUCCESS;
	}
	
	public String modifyLowerLimitBusiness(){
		if(limitBusinessVo.getLower_id() != 0){
			configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
			/**根据渠道ID获得该渠道下所有的渠道省份管控*/
			List<String> provinceList = limitBusinessDao.getLowerLimitBusinessProvinceById(limitBusinessVo.getLower_id());
			
			String provinces = "";
			if(provinceList != null && provinceList.size() > 0){
				for(String province : provinceList){
					if(!"".equals(province)){
						provinces += province + ",";
					}
				}
				if(!"".equals(provinces)){
					provinces = provinces.substring(0, provinces.length() - 1);
				}
			}
			
			limitBusinessVo.setProvince(provinces);
		}
		
		return SUCCESS;
	}
	
	public String updateLowerLimitBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		if(limitBusinessVo.getLower_id() != 0){
			int count = limitBusinessDao.updateLowerLimitBusiness(limitBusinessVo);
			if(count > 0){
				logger.info(String.format("[%s]更新渠道[%s]省份管控配置成功", userInfoVo.getLogin_username(),limitBusinessVo.getLower_name()));
			}else{
				logger.info(String.format("[%s]更新渠道[%s]省份管控配置失败", userInfoVo.getLogin_username(),limitBusinessVo.getLower_name()));
			}
		}
		actionPath = "limitBusiness/getLimitBusinessList.action";
		return SUCCESS;
	}
	
	public String deleteLowerLimitBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		try {
			limitBusinessVo.setLower_name(URLDecoder.decode(limitBusinessVo.getLower_name(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		int count = limitBusinessDao.deleteLowerLimitBusiness(limitBusinessVo.getLower_id());
		if(count > 0){
			logger.info(String.format("[%s]删除渠道[%s]省份管控信息成功", platUser.getLogin_username(),limitBusinessVo.getLower_name()));
		}else{
			logger.info(String.format("[%s]删除渠道[%s]省份管控信息失败", platUser.getLogin_username(),limitBusinessVo.getLower_name()));
		}
		actionPath = "limitBusiness/getLimitBusinessList.action";
		return SUCCESS;
	}
	
	public String modifyLimitBusiness(){
		try {
			/**本地测试*/
//			String province = new String(limitCodeVo.getProvince().getBytes("ISO-8859-1"),"UTF-8");
//			limitCodeVo.setProvince(province);
			/**线上环境*/
			limitBusinessVo.setProvince(URLDecoder.decode(limitBusinessVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("保存省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(limitBusinessVo.getLower_id() != 0){
			limitBusinessVo = limitBusinessDao.getLimitBusinessByLowerIdAndProvince(limitBusinessVo.getLower_id(), limitBusinessVo.getProvince());
		}
		
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		
		return SUCCESS;
	}
	
	public String updateLimitBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		try {
			limitBusinessVo.setProvince(URLDecoder.decode(limitBusinessVo.getProvince(), "UTF-8"));
			limitBusinessVo.setLower_name(URLDecoder.decode(limitBusinessVo.getLower_name(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("保存省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(limitBusinessVo.getId() != 0){
			int count  = limitBusinessDao.updateLimitBusiness(limitBusinessVo);
			if(count > 0){
				logger.info(String.format("[%s]更新渠道[%s]省份[%s]管控配置成功", userInfoVo.getLogin_username(),limitBusinessVo.getLower_name(),limitBusinessVo.getProvince()));
			}else{
				logger.info(String.format("[%s]更新渠道[%s]省份[%s]管控配置失败", userInfoVo.getLogin_username(),limitBusinessVo.getLower_name(),limitBusinessVo.getProvince()));
			}
		}
		actionPath = "limitBusiness/getLimitBusinessList.action";
		return SUCCESS;
	}
	
	public String deleteLimitBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		try {
			limitBusinessVo.setProvince(URLDecoder.decode(limitBusinessVo.getProvince(), "UTF-8"));
			limitBusinessVo.setLower_name(URLDecoder.decode(limitBusinessVo.getLower_name(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码省份管控信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = limitBusinessDao.deleteLimitBusiness(limitBusinessVo.getLower_id(), limitBusinessVo.getProvince());
		if(count > 0){
			logger.info(String.format("[%s]删除渠道[%s]省份[%s]管控信息成功", platUser.getLogin_username(),limitBusinessVo.getLower_name(),limitBusinessVo.getProvince()));
		}else{
			logger.info(String.format("[%s]删除渠道[%s]省份[%s]管控信息失败", platUser.getLogin_username(),limitBusinessVo.getLower_name(),limitBusinessVo.getProvince()));
		}
		actionPath = "limitBusiness/getLimitBusinessList.action";
		return SUCCESS;
	}
	
	public ILimitBusinessDao getLimitBusinessDao() {
		return limitBusinessDao;
	}
	public void setLimitBusinessDao(ILimitBusinessDao limitBusinessDao) {
		this.limitBusinessDao = limitBusinessDao;
	}
	public List<LimitBusinessMapVo> getLimitBusinessMapVoList() {
		return limitBusinessMapVoList;
	}
	public void setLimitBusinessMapVoList(List<LimitBusinessMapVo> limitBusinessMapVoList) {
		this.limitBusinessMapVoList = limitBusinessMapVoList;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public LimitBusinessVo getLimitBusinessVo() {
		return limitBusinessVo;
	}
	public void setLimitBusinessVo(LimitBusinessVo limitBusinessVo) {
		this.limitBusinessVo = limitBusinessVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
